package views;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JButton;
import javax.swing.JSeparator;

import controllers.BagageAfhandelController;
import controllers.CheckinOpvragenController;
import controllers.CheckinPlaatsReserverenController;
import core.logic.services.utilServices.ValidatieService;
import core.models.dataModels.CheckinOpvragenModel;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.awt.Dimension;

// @Author Tim Sterckx

public class CheckinView extends JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTextField txtETicketnr;
	private JTextField txtNaam;
	private JTextField txtVoornaam;
	private JTextField txtPaspoortnr;
	private JTextField txtVluchtNr;
	private JButton btnPlaatsReserveren;
	private JButton btnBagageAfhandeling;

	private CheckinOpvragenModel model;

	public CheckinOpvragenModel get_model() {
		return model;
	}

	public void set_model(CheckinOpvragenModel _model) {
		this.model = _model;
	}

	public CheckinOpvragenModel get_resultModel() {
		return _resultModel;
	}

	public void set_resultModel(CheckinOpvragenModel _resultModel) {
		this._resultModel = _resultModel;
	}

	private CheckinOpvragenModel _resultModel;

	public CheckinView(final CheckinOpvragenModel avmmodel) {
		setTitle("Aanmelden Check-in");
		this.model = avmmodel;
		JLabel lblEticketnr = new JLabel("E-ticketnr:");

		txtETicketnr = new JTextField();
		txtETicketnr.setColumns(10);

		final JButton btnControleer = new JButton("Controleer");
		btnControleer.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				if (evt.getSource() == btnControleer) {

					if (ValidatieService.isPresent(txtETicketnr, "ETicketnr")) {
						if (ValidatieService.isInteger(txtETicketnr, "ETicketnr")) {
							model.setGeselecteerdTicketID((Integer.valueOf(txtETicketnr.getText())));

							CheckinOpvragenController.handleView(model);

							try {
								txtVoornaam.setText(model.getPassagier().getPersoon().getVoornaam());
								txtNaam.setText(model.getPassagier().getPersoon().getNaam());
								txtVluchtNr.setText(model.getPassagier().getVlucht().getVluchtNr());
								txtPaspoortnr.setText(model.getPassagier().getPaspoortNr());

								// @Author: Gerrit Beyens
								btnPlaatsReserveren.setEnabled(true);
								btnBagageAfhandeling.setEnabled(true);
							} catch (NullPointerException npe) {
								JOptionPane.showMessageDialog(null, "Deze e-ticketnr is niet toegekend.", "Fout",
										JOptionPane.ERROR_MESSAGE);
								geenETicket();
							}

						} else {
							geenETicket();
						}

					} else {
						geenETicket();
					}
				}
			}
		});

		JSeparator separator = new JSeparator();

		JLabel lblNaam = new JLabel("Naam:");

		JLabel lblVoornaam = new JLabel("Voornaam:");

		txtNaam = new JTextField();
		txtNaam.setEditable(false);
		txtNaam.setColumns(10);
		// haalt de Achternaam op van de passagier met het gekoppelde Eticket

		txtVoornaam = new JTextField();
		txtVoornaam.setEditable(false);
		txtVoornaam.setColumns(10);
		txtVoornaam.setText(model.getVoornaam());

		JLabel LblPaspoortNr = new JLabel("Paspoortnr :");

		txtPaspoortnr = new JTextField();
		txtPaspoortnr.setEditable(false);
		txtPaspoortnr.setColumns(10);

		JLabel lblVluctNr = new JLabel("Vluchtnr :");

		txtVluchtNr = new JTextField();
		txtVluchtNr.setEditable(false);
		txtVluchtNr.setColumns(10);

		btnBagageAfhandeling = new JButton("Bagage Afhandelen");
		btnBagageAfhandeling.setMinimumSize(new Dimension(140, 23));
		btnBagageAfhandeling.setMaximumSize(new Dimension(140, 23));
		btnBagageAfhandeling.setEnabled(false);
		btnBagageAfhandeling.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int aantal = Integer.parseInt(JOptionPane.showInputDialog(null, "Aantal bagage? ", "Aantal bagage",
						JOptionPane.QUESTION_MESSAGE));
				BagageAfhandelController.showView(aantal, model);
			}
		});

		JButton btnVoltooien = new JButton("Voltooien");
		btnVoltooien.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (model.getPassagier().getZitplaats() != null) {
					JOptionPane.showMessageDialog(txtNaam, "Passagier is ingecheckt");

					CheckinOpvragenController.closeView();
				} else {
					JOptionPane.showMessageDialog(txtNaam, "Passagier heeft nog geen zitplaats");
				}

			}
		});

		JButton btnPrintVluchtticket = new JButton("Print Vluchtticket");
		btnPrintVluchtticket.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				PrinterJob pj = PrinterJob.getPrinterJob();

				if (pj.printDialog()) {
					try {
						pj.print();
					} catch (PrinterException exc) {
						System.out.println(exc);
					}
				}

			}
		});

		JButton btnAnnuleren = new JButton("Annuleren");
		btnAnnuleren.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				CheckinOpvragenController.closeView();
			}
		});

		btnPlaatsReserveren = new JButton("Plaats Reserveren");
		btnPlaatsReserveren.setMaximumSize(new Dimension(140, 140));
		btnPlaatsReserveren.setEnabled(false);
		btnPlaatsReserveren.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				CheckinPlaatsReserverenController.showView(model.getPassagier());
			}
		});

		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(groupLayout
				.createParallelGroup(Alignment.LEADING)
				.addGroup(
						groupLayout
								.createSequentialGroup()
								.addGap(22)
								.addGroup(
										groupLayout
												.createParallelGroup(Alignment.LEADING)
												.addComponent(separator, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
														GroupLayout.PREFERRED_SIZE)
												.addGroup(
														groupLayout
																.createSequentialGroup()
																.addGroup(
																		groupLayout
																				.createParallelGroup(Alignment.LEADING)
																				.addComponent(lblNaam)
																				.addComponent(lblVoornaam)
																				.addComponent(LblPaspoortNr)
																				.addComponent(lblVluctNr, GroupLayout.PREFERRED_SIZE, 60,
																						GroupLayout.PREFERRED_SIZE))
																.addGap(33)
																.addGroup(
																		groupLayout
																				.createParallelGroup(Alignment.LEADING)
																				.addComponent(txtVluchtNr, GroupLayout.PREFERRED_SIZE,
																						GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
																				.addComponent(txtPaspoortnr, GroupLayout.PREFERRED_SIZE,
																						GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
																				.addComponent(txtVoornaam, GroupLayout.PREFERRED_SIZE,
																						GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
																				.addComponent(txtNaam, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
																						GroupLayout.PREFERRED_SIZE)))
												.addGroup(
														groupLayout
																.createSequentialGroup()
																.addGroup(
																		groupLayout
																				.createParallelGroup(Alignment.LEADING)
																				.addGroup(
																						groupLayout
																								.createSequentialGroup()
																								.addComponent(lblEticketnr)
																								.addGap(18)
																								.addComponent(txtETicketnr, GroupLayout.PREFERRED_SIZE,
																										GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
																				.addComponent(btnBagageAfhandeling, GroupLayout.PREFERRED_SIZE, 143,
																						GroupLayout.PREFERRED_SIZE))
																.addGap(39)
																.addGroup(
																		groupLayout
																				.createParallelGroup(Alignment.LEADING)
																				.addComponent(btnPlaatsReserveren, GroupLayout.PREFERRED_SIZE, 143,
																						GroupLayout.PREFERRED_SIZE).addComponent(btnControleer))))
								.addContainerGap(85, Short.MAX_VALUE))
				.addGroup(
						groupLayout.createSequentialGroup().addContainerGap().addComponent(btnVoltooien)
								.addPreferredGap(ComponentPlacement.UNRELATED)
								.addComponent(btnAnnuleren, GroupLayout.DEFAULT_SIZE, 104, Short.MAX_VALUE).addGap(83)
								.addComponent(btnPrintVluchtticket, GroupLayout.PREFERRED_SIZE, 137, GroupLayout.PREFERRED_SIZE)
								.addGap(22)));
		groupLayout.setVerticalGroup(groupLayout.createParallelGroup(Alignment.LEADING).addGroup(
				groupLayout
						.createSequentialGroup()
						.addGap(24)
						.addGroup(
								groupLayout
										.createParallelGroup(Alignment.BASELINE)
										.addComponent(lblEticketnr)
										.addComponent(txtETicketnr, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
												GroupLayout.PREFERRED_SIZE).addComponent(btnControleer))
						.addGap(20)
						.addComponent(separator, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addGap(27)
						.addGroup(
								groupLayout
										.createParallelGroup(Alignment.BASELINE)
										.addComponent(lblNaam)
										.addComponent(txtNaam, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
												GroupLayout.PREFERRED_SIZE))
						.addPreferredGap(ComponentPlacement.UNRELATED)
						.addGroup(
								groupLayout
										.createParallelGroup(Alignment.BASELINE)
										.addComponent(lblVoornaam)
										.addComponent(txtVoornaam, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
												GroupLayout.PREFERRED_SIZE))
						.addGap(18)
						.addGroup(
								groupLayout
										.createParallelGroup(Alignment.BASELINE)
										.addComponent(LblPaspoortNr)
										.addComponent(txtPaspoortnr, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
												GroupLayout.PREFERRED_SIZE))
						.addGap(18)
						.addGroup(
								groupLayout
										.createParallelGroup(Alignment.BASELINE)
										.addComponent(lblVluctNr)
										.addComponent(txtVluchtNr, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
												GroupLayout.PREFERRED_SIZE))
						.addGap(33)
						.addGroup(
								groupLayout.createParallelGroup(Alignment.BASELINE)
										.addComponent(btnBagageAfhandeling, GroupLayout.PREFERRED_SIZE, 39, GroupLayout.PREFERRED_SIZE)
										.addComponent(btnPlaatsReserveren, GroupLayout.PREFERRED_SIZE, 39, GroupLayout.PREFERRED_SIZE))
						.addPreferredGap(ComponentPlacement.RELATED, 38, Short.MAX_VALUE)
						.addGroup(
								groupLayout.createParallelGroup(Alignment.BASELINE).addComponent(btnVoltooien)
										.addComponent(btnPrintVluchtticket).addComponent(btnAnnuleren)).addGap(30)));
		getContentPane().setLayout(groupLayout);
	}

	// @Author: Gerrit Beyens
	private void geenETicket() {
		txtVoornaam.setText("");
		txtNaam.setText("");
		txtVluchtNr.setText("");
		txtPaspoortnr.setText("");
		btnPlaatsReserveren.setEnabled(false);
		btnBagageAfhandeling.setEnabled(false);
	}

}
